package gl.web.onlinedoc.controller;


import gl.web.apk.common.config.Config;
import gl.web.onlinedoc.search.SearchManager;
import gl.web.onlinedoc.search.SearchResult;
import gl.web.onlinedoc.util.FileSort;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;


import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Page;

public class OnlineDocController extends Controller {
	Log log = Log.getLog(getClass());
	public static volatile boolean isDocIndexed = false;
	public void index() {
		render("index.html");
	}

	public void dir(){
		ArrayList<String> fileNameArray_ = new ArrayList<String>();
		final File rootdir = new File(Config.getWebRootPath()+"/doc");
		genDirTree(fileNameArray_, rootdir);
		
		ArrayList<String> fileNameArray = new ArrayList<String>();
		for (int i = 0; i < fileNameArray_.size(); i++) {
			final String string = fileNameArray_.get(i);
			log.info("string:"+string);
			String substring = string.substring(string.indexOf("doc")+4);
			substring = substring.replace("\\", "/");
			fileNameArray.add(substring);
			log.info("substring:"+substring);
		}
		Collections.sort(fileNameArray, new Comparator<String>(){

			@Override
			public int compare(String o1, String o2) {
				return FileSort.myCompare(o1, o2);
			}
			
		});
		Page<String> page = new Page<String>(fileNameArray, 0, 30, 1, 1); 
		setAttr("page", page);
		render("dir.html");
	}
	public void genDirTree(ArrayList<String> fileNameArray, final File rootdir) {
		if (rootdir.isDirectory()) {
			File[] files = rootdir.listFiles();
			for (File file : files) {
				if (file.isDirectory()) {
					genDirTree(fileNameArray, file);
				}else{
					fileNameArray.add(file.getAbsolutePath()) ; 
				}
			}
		}else{
			fileNameArray.add(rootdir.getAbsolutePath()); 
		}
	}
	public void file(){
		String filename = getPara("filename");
		log.info("filename:"+filename);
		if (filename==null||filename.length()<=0) {
			renderText("filename is null");
		}else{
			render("../doc/"+filename);
		}
	}
	
	public void search(){
		String par = getPara("q","");
		if (par==null||par.length()<=0) {
			redirect("/onlinedoc/dir");
			return;
		}
		if (!isDocIndexed) {
			synchronized (this) {
				isDocIndexed = true;
				SearchManager.createIndex(Config.getDocDir().toString(),Config.getWebRootPath()+"/temp/search/");
			}
		}
		final List<SearchResult> searchIndex = SearchManager.searchIndex(par);
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < searchIndex.size(); i++) {
			SearchResult r = searchIndex.get(i);
			
			String substring = r.filepath.substring(r.filepath.indexOf("doc")+4);
			substring = substring.replace("\\", "/");
			r.uri = substring;
			System.out.println(r.highLight);
			System.out.println(r.uri);
			sb.append("+++++++++++++++++++++++++++</br>");
			sb.append("<a href=\"/onlinedoc?name="+r.uri+"\">"+r.uri+"</a></br>");
			sb.append("---------------------------</br>");
			sb.append(r.highLight);
			sb.append("</br>");
			sb.append("---------------------------</br>");
		}
		Page<SearchResult> page = new Page<SearchResult>(searchIndex, 0, 30, 1, 1); 
		setAttr("page", page);
		renderHtml(sb.toString());
		
	}
}


